package com.wz.utils.transaction.test;


import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


/**
 * Created by xsls on 2019/6/17.
 *
 * @Mapper是mybatis的注解，标注在dao层接口上，可以通过动态代理生成接口的实例bean，启动类不需要添加@MapperScan包扫描
 *
 * @Repository是spring的注解，也标注在dao层接口上，启动类需要加@MapperScan包扫描，生成对应的bean，加了@MapperScan就相当于给包中的接口都加上了@Mapper，同时@Repository还有消除“注入”的错误提示的作用
 */
@EnableTransactionManagement
@EnableAspectJAutoProxy//(exposeProxy = true)
@MapperScan("com.wz.utils.transaction")
public class MainConfig {


    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("wz19910127");
        dataSource.setUrl("jdbc:mysql://localhost:3306/tuling");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }



    /**
     * 配置JdbcTemplate Bean组件
     * <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
     * <property name="dataSource" ref="dataSource" ></property>
     * </bean>
     *
     * @param dataSource
     * @return
     */
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 配置事务管理器
     * <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
     * <property name="dataSource" ref="dataSource"></property>
     * </bean>
     *
     * @param dataSource
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }





}
